home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet Info 1994 March
/
Internet Info CD-ROM (Walnut Creek) (March 1994).iso
/
networking
/
ip
/
ka9q
/
src.arc
/
SESSION.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-08-19
|
8KB
|
403 lines
/* Session control */
#include <stdio.h>
#include "global.h"
#include "config.h"
#include "mbuf.h"
#include "socket.h"
#include "ftpcli.h"
#include "telnet.h"
#include "icmp.h"
#include "ax25tnc.h"
#include "session.h"
#include "cmdparse.h"
#include "timer.h"
#include "proc.h"
#include "tty.h"
#include "commands.h"
struct session *Sessions;
struct session *Current;
int Mode = CMD_MODE;
char Notval[] = "Not a valid control block\n";
static char Badsess[] = "Invalid session\n";
char *Sestypes[] = {
"",
"Telnet",
"FTP",
"AX25",
"Finger",
"Ping",
"NET/ROM"
};
/* Convert a character string containing a decimal session index number
* into a pointer. If the arg is NULLCHAR, use the current default session.
* If the index is out of range or unused, return NULLSESSION.
*/
struct session *
sessptr(cp)
char *cp;
{
register struct session *sp;
unsigned int i;
if(cp == NULLCHAR){
sp = Current;
} else {
i = (unsigned)atoi(cp);
if(i >= Nsessions)
sp = NULLSESSION;
else
sp = &Sessions[i];
}
if(sp == NULLSESSION || sp->type == FREE)
sp = NULLSESSION;
return sp;
}
/* Select and display sessions */
int
dosession(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct session *sp;
struct sockaddr fsocket;
int i,k,s;
int r,t;
char *cp;
sp = (struct session *)p;
if(argc > 1){
if((sp = sessptr(argv[1])) != NULLSESSION){
go(0,NULL,sp);
} else
printf("Session %s not active\n",argv[1]);
return 0;
}
printf(" # S# Type Rcv-Q Snd-Q State Remote socket\n");
for(sp=Sessions; sp < &Sessions[Nsessions];sp++){
if(sp->type == FREE)
continue;
i = SOCKSIZE;
s = sp->s;
k = getpeername(s,(char *)&fsocket,&i);
r = socklen(s,0);
t = socklen(s,1);
cp = sockstate(s);
printf("%c%-3u%-4d%-8s%6d%6d %-13s",
(Current == sp)? '*':' ',
(unsigned)(sp - Sessions),
s,
Sestypes[sp->type],
r,
t,
(cp != NULLCHAR) ? cp : "");
if(sp->name != NULLCHAR)
printf("%s ",sp->name);
if(k == 0)
printf("(%s)",psocket(&fsocket));
if(sp->rfile != NULLCHAR || sp->ufile != NULLCHAR)
printf("\t");
if(sp->rfile != NULLCHAR)
printf("Record: %s ",sp->rfile);
if(sp->ufile != NULLCHAR)
printf("Upload: %s",sp->ufile);
printf("\n");
if(sp->type == FTP && (s = sp->cb.ftp->data) != -1){
/* Display data channel, if any */
i = SOCKSIZE;
k = getpeername(s,(char *)&fsocket,&i);
r = socklen(s,0);
t = socklen(s,1);
cp = sockstate(s);
printf(" %-4d%-8s%6d%6d %-13s%s",
s,
Sestypes[sp->type],
r,
t,
(cp != NULLCHAR) ? cp : "",
(sp->name != NULLCHAR) ? sp->name : "");
if(k == 0)
printf(" (%s)",psocket(&fsocket));
printf("\n");
}
}
return 0;
}
/* Resume current session, and wait for it */
int
go(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct session *sp;
sp = (struct session *)p;
if(sp == NULLSESSION || sp->type == FREE)
return 0;
Current = sp;
ttysetmode(sp->ttymode);
Mode = CONV_MODE;
psignal(sp,0);
switch(sp->type){
case TELNET:
pwait(sp->cb.telnet->output);
break;
case FTP:
pwait(sp->cb.ftp->output);
break;
case NRSESSION:
case AX25TNC:
pwait(sp->cb.ax25->output);
break;
case PING:
pwait(sp->cb.ping->proc);
break;
}
return 0;
}
int
doclose(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct session *sp;
sp = (struct session *)p;
if(argc > 1)
sp = sessptr(argv[1]);
if(sp == NULLSESSION){
printf(Badsess);
return -1;
}
shutdown(sp->s,1);
return 0;
}
int
doreset(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct session *sp;
sp = (struct session *)p;
if(argc > 1)
sp = sessptr(argv[1]);
if(sp == NULLSESSION){
printf(Badsess);
return -1;
}
/* Unwedge anyone waiting for a domain resolution, etc */
switch(sp->type){
case FTP:
alert(sp->cb.ftp->output,-1);
break;
case TELNET:
alert(sp->cb.telnet->output,-1);
break;
case FINGER:
alert(sp->cb.finger,-1);
break;
case AX25TNC:
break;
case PING:
alert(sp->cb.ping->proc,-1);
break;
case NRSESSION:
break;
}
shutdown(sp->s,2);
if(sp->type == FTP)
shutdown(sp->cb.ftp->data,2);
return 0;
}
int
dokick(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct session *sp;
sp = (struct session *)p;
if(argc > 1)
sp = sessptr(argv[1]);
if(sp == NULLSESSION){
printf(Badsess);
return -1;
}
sockkick(sp->s);
if(sp->type == FTP)
sockkick(sp->cb.ftp->data);
return 0;
}
struct session *
newsession(name,type)
char *name;
int type;
{
register struct session *sp;
for(sp=Sessions;sp < &Sessions[Nsessions];sp++){
if(sp->type == FREE){
sp->type = type;
sp->s = -1;
if(name != NULLCHAR)
sp->name = strdup(name);
return sp;
}
}
return NULLSESSION;
}
void
freesession(sp)
struct session *sp;
{
if(sp == NULLSESSION)
return;
if(sp->s != -1)
close_s(sp->s);
free_q(&sp->input);
if(sp->record != NULLFILE){
fclose(sp->record);
sp->record = NULLFILE;
}
free(sp->rfile);
sp->rfile = NULLCHAR;
if(sp->upload != NULLFILE){
fclose(sp->upload);
sp->upload = NULLFILE;
}
free(sp->ufile);
sp->ufile = NULLCHAR;
free(sp->name);
sp->name = NULLCHAR;
sp->type = FREE;
if(Current == sp){
Current = NULLSESSION;
Mode = CMD_MODE;
}
}
/* Control session recording */
int
dorecord(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct session *sp;
sp = (struct session *)p;
if(sp == NULLSESSION){
printf("No current session\n");
return 1;
}
if(argc > 1){
if(sp->rfile != NULLCHAR){
fclose(sp->record);
free(sp->rfile);
sp->record = NULLFILE;
sp->rfile = NULLCHAR;
}
/* Open new record file, unless file name is "off", which means
* disable recording
*/
if(strcmp(argv[1],"off") != 0){
if((sp->record = fopen(argv[1],APPEND_TEXT)) == NULLFILE)
printf("Can't open %s: %s\n",argv[1],sys_errlist[errno]);
else
sp->rfile = strdup(argv[1]);
}
}
if(sp->rfile != NULLCHAR)
printf("Recording into %s\n",sp->rfile);
else
printf("Recording off\n");
return 0;
}
/* Control file transmission */
int
doupload(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct telnet *tn;
struct ax25tnc *axp;
struct session *sp;
sp = (struct session *)p;
if(sp == NULLSESSION){
printf("No current session\n");
return 1;
}
if(argc < 2){
if(sp->ufile != NULLCHAR)
printf("Uploading %s\n",sp->ufile);
else
printf("Uploading off\n");
return 0;
}
switch(sp->type){
case TELNET:
tn = sp->cb.telnet;
break;
case NRSESSION:
case AX25TNC:
axp = sp->cb.ax25;
break;
default:
printf("Uploading not supported\n");
return 1;
}
if(strcmp(argv[1],"stop") == 0 && sp->upload != NULLFILE){
/* Abort upload */
fclose(sp->upload);
sp->upload = NULLFILE;
free(sp->ufile);
sp->ufile = NULLCHAR;
if(tn != NULLTN){
killproc(tn->upload);
tn->upload = NULLPROC;
} else if(axp != NULLTNC){
killproc(axp->upload);
axp->upload = NULLPROC;
}
return 0;
}
/* Open upload file */
if((sp->upload = fopen(argv[1],READ_TEXT)) == NULLFILE){
printf("Can't read %s: %s\n",argv[1],sys_errlist[errno]);
return 1;
}
sp->ufile = strdup(argv[1]);
/* All set, invoke the upload process */
switch(sp->type){
#ifdef AX25
case NRSESSION:
case AX25TNC:
axp->upload = newproc("upload",1024,ax_upload,0,sp,NULL);
break;
#endif
case TELNET:
tn->upload = newproc("upload",1024,tel_upload,0,sp,NULL);
break;
}
return 0;
}